package com.ibiaoke.order.service;

import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.common.utils.LocationUtils;
import com.ibiaoke.order.dao.XcxAddressDao;
import com.ibiaoke.order.entity.XcxAddress;
import com.ibiaoke.order.entity.XcxAddressExample;
import com.ibiaoke.system.dao.ShopDao;
import com.ibiaoke.system.entity.Shop;
import com.ibiaoke.system.service.AreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ibiaoke.order.entity.vo.XcxAddressVo;

import java.util.Date;
import java.util.List;

@Service
public class XcxAddressServiceImpl implements  XcxAddressService{

    @Autowired
    private AreaService areaService;

    @Autowired
    private XcxAddressDao xcxAddressDao;

    @Autowired
    private ShopDao shopDao;


    @Override
    public int saveXcxAddress(XcxAddressVo xcxAddressVO) {
        XcxAddress XcxAddress = new XcxAddress();
        BeanUtils.copyProperties(xcxAddressVO,XcxAddress);
        XcxAddress.setCreateTime(new Date());
        XcxAddress.setUpdateTime(new Date());
        XcxAddress.setDelStatus((byte) CommonConsts.NO);
        fillAreaName(XcxAddress);
        return xcxAddressDao.insertSelective(XcxAddress);
    }

    @Override
    public List<XcxAddress> listXcxAddress(Integer customerId) {
        return xcxAddressDao.listXcxAddress(customerId);
    }


    @Override
    public XcxAddress getXcxAddress(Integer addressId) {
        AssertUtils.notNull(addressId, "地址Id不能为空");
        XcxAddressExample example = new XcxAddressExample();
        XcxAddressExample.Criteria criteria = example.createCriteria();
        criteria.andAddressIdEqualTo(addressId);
        criteria.andDelStatusEqualTo((byte) CommonConsts.NO);
        XcxAddress xcxAddress = xcxAddressDao.getByExample(example);
        AssertUtils.notNull(xcxAddress, "查不到地址");
        return xcxAddress;
    }

    @Override
    public int updateXcxAddress(XcxAddressVo xcxAddressVO) {
        XcxAddress xcxAddress = new XcxAddress();
        AssertUtils.notNull(xcxAddressVO.getAddressId(), "地址Id不能为空");
        BeanUtils.copyProperties(xcxAddressVO, xcxAddress);
        xcxAddress.setUpdateTime(new Date());
        fillAreaName(xcxAddress);
        return xcxAddressDao.updateByPrimaryKeySelective(xcxAddress);
    }



    @Override
    public int deleteOrderAddress(Integer addressId) {
        AssertUtils.notNull(addressId, "地址Id不能为空");
        return xcxAddressDao.delete(addressId);
    }

    @Override
    public Double getDistinceByShopAndAddress(XcxAddress xcxAddress, Integer shopid) {
        Shop shop = shopDao.selectByPrimaryKey(shopid);
        Double distance = LocationUtils.getDistance(xcxAddress.getLongitude(), xcxAddress.getLatitude(), shop.getDesLongitude(), shop.getDesLatitude());
        return distance/1000;
    }

    private void fillAreaName(XcxAddress xcxAddress) {
        String provinceName = areaService.getProvinceName(xcxAddress.getProvinceId());
        String cityName = areaService.getCityName(xcxAddress.getCityId());
        String districtName = areaService.geDistrictName(xcxAddress.getDistrictId());
        xcxAddress.setProvinceName(provinceName);
        xcxAddress.setCityName(cityName);
        xcxAddress.setDistrictName(districtName);
    }
}
